/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.icq.io;

import java.awt.Component;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import org.apache.log4j.Logger;

/**
 * Установка темы приложения.
 * @author Носов А.В.
 */
public class loadTheme {

    // Variables declaration
    private static final Logger log = Logger.getLogger(loadTheme.class);
    
    private String res = System.getProperty("user.dir") + File.separator;
    private String theme_name = "";
    
    private Properties language = new Properties();
    private Properties icons = new Properties();
    private Properties sound = new Properties();
    private Properties theme = new Properties();
    // End of variables declaration

    public loadTheme(String theme, String language) {
        try {
            this.language.load(new FileInputStream(res + "Languages" + File.separator + language + ".properties"));
        } catch (IOException ex) {
//            this.language = null;
            log.error("Не загрузился язык. ", ex);
        }
        
        try {
            theme_name = theme;
            this.theme.load(new FileInputStream(res + "Themes" + File.separator + theme + File.separator + theme + ".properties"));
        } catch (IOException ex) {
//            this.theme = null;
            log.error("Не загрузилась тема. ", ex);
        }
        
        try {
            this.icons.load(new FileInputStream(res + "Themes" + File.separator + theme + File.separator + "Icons.properties"));
        } catch (IOException ex) {
//            this.icons = null;
            log.error("Не загрузились иконки. ", ex);
        }
        
        try {
            this.sound.load(new FileInputStream(res + "Themes" + File.separator + theme + File.separator + "Sounds.properties"));
        } catch (IOException ex) {
//            this.sound = null;
            log.error("Не загрузилась музыка. ", ex);
        }
    }

    public void setTheme(Component obj) {
        Object name = null;
        try {
//            System.out.print("metod - " + obj.getClass().getMethod("getName").getName() + ": ");
            name = obj.getClass().getMethod("getName").invoke(obj);
            //if ((name!=null) && (name.equals("menu.treeLeaf")))
//                log.info(name);
            if (name != null) {
                
                String text = null;
                // icon
                if (icons.getProperty(name.toString()) != null) {
                    Icon icon = getIcon(name.toString());
                    obj.getClass().getMethod("setIcon", Icon.class).invoke(obj, icon);
                }
                // title
                if (language.getProperty(name.toString() + ".title") != null) {
                    text = language.getProperty(name.toString() + ".title");
                    obj.getClass().getMethod("setTitle", text.getClass()).invoke(obj, text);
                }
                // text
                if (language.getProperty(name.toString() + ".text") != null) {
                    text = language.getProperty(name.toString() + ".text");
                    obj.getClass().getMethod("setText", text.getClass()).invoke(obj, text);
                }
                // toolTipText
                if (language.getProperty(name.toString() + ".toolTipText") != null) {
                    text = language.getProperty(name.toString() + ".toolTipText");
                    obj.getClass().getMethod("setToolTipText", text.getClass()).invoke(obj, text);
                }
                
//                String n = name.toString();
//                n = n.substring(0, n.indexOf("."));
                // font
                
                // font.size
                
                // bg
                
            }
        } catch (NoSuchMethodException ex) {
            log.error("Метод: " + ex.getMessage() + " не найден.");
        }
        catch (IllegalAccessException ex) {
            log.error("IllegalAccessException - " + ex.getMessage());
        }
        catch (IllegalArgumentException ex) {
            log.error("IllegalArgumentException - " + ex.getMessage(), ex);
        }
        catch (InvocationTargetException ex) {
            log.error("InvocationTargetException - " + ex.getMessage());
        }
        
        if (obj instanceof Container) {
            Container container = (Container) obj;
            Component[] components = container.getComponents(); 
            for (Component component : components) {
                setTheme(component);
            }
        }
    }
    
    public ImageIcon getIcon(String name) {
        try {
            ImageIcon icon = null;
            String path = icons.getProperty(name);
            if (path.endsWith(".png")) {
                icon = new ImageIcon(res + "Themes" + File.separator + theme_name + File.separator +
                                     "Icons" + File.separator + path);
                return icon;

            } else {
                if (path.equals("")) return icon;
                String[] s = path.split(";");
                int[] pes = new int[4];
                for (int i=0; i<s.length; i++)
                    pes[i] = Integer.valueOf(s[i]);

                File f = new File(res + "Themes" + File.separator + theme_name + File.separator +
                                  "Icons" + File.separator + 
                                  icons.getProperty(name.substring(0, name.indexOf("."))));

                BufferedImage im = ImageIO.read(f);
                ImageFilter filter = new CropImageFilter(pes[0], pes[1], pes[2], pes[3]);
                ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
                icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(ip));
                return icon;
            }
        } catch (IOException ex) {
            log.error("Нет файла иконки.", ex);
            return null;
        }
    }

    public String getLanguage(String name) {
        return language.getProperty(name);
    }
}
